<?php

namespace Admin\Controller;

use Utuo\BackgroundController;
use Utuo\Phpass;

class AdminController extends BackgroundController {
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('管理员管理');
		$this->setActionTitle(array(
				'index' => '管理员列表',
				'add' => '添加管理员',
				'modify' => '修改管理员',
				'recycle' => '回收站'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	// 管理员列表
	public function indexAction() {
		$this->getAdminLists(0);
	}
	
	// 管理员回收站
	public function recycleAction() {
		$this->getAdminLists(1);
	}
	
	// 添加管理员
	public function addAction(){
		$this->display();
	}
	
	// 获取列表
	private function getAdminLists($is_deleted) {
		$condition = array(
				'is_deleted' => $is_deleted
		);
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$adminLists = M('admin')->where($condition)->limit($pagesize)->page($page)->order('id')->select();
		$count = M('admin')->where($condition)->count();
		$this->assign('adminLists', $adminLists);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	// 提交添加
	public function addPostAction() {
		$m = M('admin');
		if ($m->create()) {
			$m->password = Phpass::hash($m->password);
			if ($m->add()) {
				$this->ajaxSuccess('添加成功');
			} else {
				$this->ajaxError('添加失败');
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	// 修改
	public function modifyAction() {
		$admin = $this->getOneById('admin');
		$this->assign('admin', $admin);
		$this->display();
	}
	
	// 提交修改
	public function modifyPostAction() {
		$m = M("admin");
		if($m->create()){
			$m->password = Phpass::hash($m->password);
			if($m->save()){
				$this->ajaxSuccess("保存成功");
			}else{
				$this->ajaxError();
			}
		}else{
			$this->ajaxError($m->getError());
		}
	}
	
	// 删除数据
	public function removePostAction() {
		$this->ajaxRemove("admin");
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete('admin');
	}
	
	// 恢复用户
	public function restorePostAction() {
		$this->ajaxRestore('admin');
	}
	
	public function setStatusPostAction() {
		$this->ajaxSetStatus('admin');
	}
	
}